package com.example.demo.controller;

import com.example.demo.exception.MyUncheckedException;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.dto.ExamFileDTO;
import com.example.demo.pojo.dto.SubjectFileDTO;
import com.example.demo.pojo.entity.ExamFile;
import com.example.demo.pojo.entity.SubjectFile;
import com.example.demo.pojo.query.ExamQuery;
import com.example.demo.pojo.vo.ExamFileShowVO;
import com.example.demo.pojo.vo.ExamVO;
import com.example.demo.service.imply.IExamFileServiceImply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fjh
 *
 */
@RestController
@RequestMapping("/examFile")
@Slf4j
@Api(tags = "试卷文件接口")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080"}, allowCredentials = "true")
public class ExamFileController {
    private final IExamFileServiceImply examFileService;
    @ApiOperation("初始化文件列表")
    @PostMapping("/init")
    public Result initFile(int subjectId) {
        List<ExamFileShowVO> result = examFileService.initFile(subjectId);
        if (result.isEmpty()) {
            return Result.error("初始化失败");
        }
        return Result.success(result);
    }
    @ApiOperation("过滤卷子与文件列表")
    @PostMapping("/filter")
    public Result filterExam(ExamQuery examQuery) {
        List<ExamVO> result = examFileService.filterExam(examQuery);
        if (result == null) {
            return Result.error("初始化失败");
        }
        return Result.success(result);
    }
    @ApiOperation("新键文件夹")
    @PostMapping("/add")
    public Result filter(ExamFileDTO examFileDTO){
        int result=examFileService.addFile(examFileDTO);
        if(result==1){
            return Result.success("创建成功");
        }
        return Result.error("创建失败");
    }
    @ApiOperation("更新文件")
    @PostMapping("/update")
    public Result updateFile(ExamFileDTO examFileDTO){
        int result=examFileService.updateFile(examFileDTO);
        if(result==0){
            return Result.error("更新失败");
        }
        return Result.success(result);
    }

    @ApiOperation("删除文件")
    @PostMapping("/delete")
    public Result deleteFile(int fileId) throws MyUncheckedException {
        boolean flag=examFileService.deleteFile(fileId);
        if(!flag){
            return Result.error("删除失败");
        }
        return Result.success();
    }

    @ApiOperation("复制文件")
    @PostMapping("/copy")
    public Result copyFile(int examFileId,int goalExamFileId){
        if(examFileId==goalExamFileId){
            return Result.error("你选择了2个相同的目录");
        }
        ExamFile subjectFile=examFileService.copyExamFile(examFileId,goalExamFileId);
        return Result.success(subjectFile);
    }

    @ApiOperation("移动文件")
    @PostMapping("/shift")
    public Result shiftFile(int examFileId,int goalExamFileId){
        if(examFileId==goalExamFileId){
            return Result.error("你选择了2个相同的目录");
        }
        ExamFile subjectFile=examFileService.shiftSubjectFile(examFileId,goalExamFileId);
        return Result.success(subjectFile);
    }

    @ApiOperation("根据Id进行查找文件")
    @PostMapping("/searchById")
    public Result searchById(int fileId){
        ExamFile subjectFile=examFileService.searchById(fileId);
        return Result.success(subjectFile);
    }
}
